package com.rental.web.controller;

import com.rental.common.login.GetIdByToken;
import com.rental.common.result.AjaxResult;
import com.rental.common.util.AuthContextHolder;
import com.rental.model.dto.UserInfoDto;
import com.rental.model.entity.user.UserInfo;
import com.rental.model.form.user.GetRecordForm;
import com.rental.model.form.user.PhoneAndCodeForm;
import com.rental.model.vo.house.ReleaseVo;
import com.rental.model.vo.user.RecordVo;
import com.rental.web.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取登录信息
     *
     * @return 用户信息
     */
    @Operation(summary = "获取登录信息")
    @GetIdByToken
    @GetMapping("/getUserInfo")
    public AjaxResult<UserInfo> getUserInfo() {
        return AjaxResult.success(userInfoService.getUserInfo(AuthContextHolder.getUserId()));
    }

    /**
     * 发送验证码
     *
     * @param phoneAndCodeForm 手机号
     * @return
     */
    @Operation(summary = "发送验证码")
    @PostMapping("/sendCode")
    public AjaxResult<String> sendCode(@RequestBody PhoneAndCodeForm phoneAndCodeForm) {
        String code = userInfoService.sendCode(phoneAndCodeForm);
        return AjaxResult.success(code);
    }

    /**
     * 登录
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public AjaxResult<String> login(@RequestBody PhoneAndCodeForm phoneAndCodeForm) {
        return AjaxResult.success(userInfoService.login(phoneAndCodeForm));
    }

    /**
     * 我的发布
     *
     * @param getRecordForm 请求表单
     * @return 发布信息
     */
    @GetIdByToken
    @Operation(summary = "我的发布")
    @PostMapping("/getMyRelease")
    public AjaxResult<RecordVo> getMyRelease(@RequestBody GetRecordForm getRecordForm) {
        getRecordForm.setId(AuthContextHolder.getUserId());
        return AjaxResult.success(userInfoService.getMyRelease(getRecordForm));
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询用户信息")
    @GetMapping("/getUserById/{id}")
    public AjaxResult<UserInfo> getUserById(@PathVariable Long id) {
        return AjaxResult.success(userInfoService.getUserById(id));
    }

    /**
     * 获取浏览记录
     *
     * @param getRecordForm 请求表单
     * @return 浏览记录
     */
    @GetIdByToken
    @Operation(summary = "获取浏览记录")
    @PostMapping("/getBrowseRecord")
    public AjaxResult<RecordVo> getBrowseRecord(@RequestBody GetRecordForm getRecordForm) {
        getRecordForm.setId(AuthContextHolder.getUserId());
        return AjaxResult.success(userInfoService.getBrowseRecord(getRecordForm));
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Operation(summary = "查询所有用户信息")
    @GetMapping("/getAllUserInfo")
    public AjaxResult<List<UserInfo>> getAllUserInfo(){
        return AjaxResult.success(userInfoService.getAllUserInfo());
    }

    /**
     * 获取收藏信息
     *
     * @param getRecordForm 请求表单
     * @return 收藏信息
     */
    @GetIdByToken
    @Operation(summary = "获取收藏信息")
    @PostMapping("/getCollectInfo")
    public AjaxResult<RecordVo> getCollectInfo(@RequestBody GetRecordForm getRecordForm) {
        getRecordForm.setId(AuthContextHolder.getUserId());
        return AjaxResult.success(userInfoService.getCollectInfo(getRecordForm));
    }

}
